/*
 * 
 * iFinity Software Development - http://www.ifinity.com.au
 * Copyright (c) 2006 - 2009
 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 * documentation files (the "Software"), the rights to use, copy, modify, merge, publish, distribute, sublicense
 * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
 * following conditions:

 * 1. The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 *    of the Software.  
 * 2. The software may not be claimed as the original work and may not be sold as a stand alone product

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 * 
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace DotNetNuke.Providers.SearchEngineSiteMapProvider
{
    public class SitePage
    {
        string _loc;
        DateTime _lastMod;
        ChangeFrequencies _changeFreq;
        double _priority;
        string _comment = null;
        public SitePage()
        {
        }
        public SitePage(string loc, DateTime lastMod, ChangeFrequencies changeFreq, double priority)
        {
            _loc = loc;
            _lastMod = lastMod;
            _changeFreq = changeFreq;
            _priority = priority;
        }
        public SitePage(string loc, DateTime lastMod, ChangeFrequencies changeFreq, double priority, string comment)
        {
            _loc = loc;
            _lastMod = lastMod;
            _changeFreq = changeFreq;
            _priority = priority;
            _comment = comment;
        }

        public string Loc
        {
            get { return _loc; }
            set { _loc = value; }
        }
        public DateTime LastMod
        {
            get
            {
                return _lastMod;
            }
            set
            {
                _lastMod = value;
            }
        }
        public void SetLastMod(string value)
        {
            _lastMod = DateTime.Parse(value);
        }
        public ChangeFrequencies ChangeFreq
        {
            get
            {
                return _changeFreq;
            }
            set
            {
                _changeFreq = value;
            }
        }
        public double Priority
        {
            get {
                if (_priority < 0)
                    return 0.1;
                else
                    if (_priority > 1)
                        return 1.0;
                    else
                        return _priority;
            }
            set { _priority = value; }
        }
        public string Comment
        {
            get { return _comment; }
            set {_comment = value;}
        }
        public string CommentAsComment
        {
            get
            {
                return "<!-- " + _comment + " -->";
            }
        }
        /// <summary>
        /// This date should be in ISO 8601 format. 
        /// This format allows you to omit the time portion, use YYYY-MM-DD.
        /// </summary>
        /// <param name="dateValue">string date value</param>
        /// <returns>ISO 8601 date format</returns>
        internal static string FormatISODate(string dateValue)
        {
             return FormatISODate(DateTime.Parse(dateValue));
        }

        /// <summary>
        /// This date should be in ISO 8601 format. 
        /// This format allows you to omit the time portion, use YYYY-MM-DD.
        /// </summary>
        /// <param name="dateValue">datetime date value</param>
        /// <returns>ISO 8601 date format</returns>
        internal static string FormatISODate(DateTime date)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(date.Year);
            sb.Append("-");
            if (date.Month < 10)
            {
                sb.Append("0");
            }
            sb.Append(date.Month);
            sb.Append("-");
            if (date.Day < 10)
            {
                sb.Append("0");
            }
            sb.Append(date.Day);

            return sb.ToString();
        }
        public static string ConvertChangeFrequencyToString(ChangeFrequencies changeFreq)
        {
            string retVal="";
            switch (changeFreq)
            {
                case ChangeFrequencies.always:
                    retVal = "always";
                    break;
                case ChangeFrequencies.daily:
                    retVal = "daily";
                    break;
                case ChangeFrequencies.hourly:
                    retVal = "hourly";
                    break;
                case ChangeFrequencies.monthly:
                    retVal = "monthly";
                    break;
                case ChangeFrequencies.never:
                    retVal = "never";
                    break;
                case ChangeFrequencies.weekly:
                    retVal = "weekly";
                    break;
                case ChangeFrequencies.yearly:
                    retVal = "yearly";
                    break;
            }
            return retVal;
        }
        public static ChangeFrequencies ConvertChangeFrequencyToEnum(string changeFreq)
        {
            ChangeFrequencies retVal = ChangeFrequencies.daily;
            switch (changeFreq)
            {
                case "always":
                    retVal = ChangeFrequencies.always;
                    break;
                case "daily":
                    retVal = ChangeFrequencies.daily;
                    break;
                case "hourly":
                    retVal = ChangeFrequencies.hourly;
                    break;
                case "monthly":
                    retVal = ChangeFrequencies.monthly;
                    break;
                case "never":
                    retVal = ChangeFrequencies.never;
                    break;
                case "weekly":
                    retVal = ChangeFrequencies.weekly;
                    break;
                case "yearly":
                    retVal = ChangeFrequencies.yearly;
                    break;
            }
            return retVal;
        }
        public static ChangeFrequencies ConvertChangeFrequencyToEnum(double hours)
        {
            ChangeFrequencies retVal = ChangeFrequencies.daily;
            if (hours <= 1)
            {
                retVal = ChangeFrequencies.always;
            }
            if (hours > 1 && hours <= 12)
            {
                retVal = ChangeFrequencies.hourly;
            }
            if (hours > 12 && hours <= 36)
            {
                retVal = ChangeFrequencies.daily;
            }
            if (hours > 36 && hours <= 160)
            {
                retVal = ChangeFrequencies.weekly;
            }
            if (hours > 160 && hours <= 672)
            {
                retVal = ChangeFrequencies.monthly;
            }
            if (hours > 672 && hours <= 8760)
            {
                retVal = ChangeFrequencies.yearly;
            }
            if (hours > 8760)
            {
                retVal = ChangeFrequencies.never;
            }
            return retVal;
        }
    }
    public class SitePageException : SitePage 
    {
        public SitePageException(Exception ex, bool showStackTrace)
        {
            base.Comment = ex.Message;
            if (showStackTrace)
            {
                base.Comment += "\n" + ex.StackTrace;
            }
        }
    }
    public enum ChangeFrequencies
    {
        always,
        hourly,
        daily,
        weekly,
        monthly,
        yearly,
        never
    }
    
}
